home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Weird malloc() seg-faults [Need Help!]
- Date: 24 Jan 1996 17:44:36 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e5r64$1587@ns.RezoNet.NET>
- References: <4drhfg$m9s@usenet.srv.cis.pitt.edu>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- Keywords: malloc, weird, segmentation faults, bad programming?, help
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, Jayadev Billa says...
- >I have been having problems with seg-faults with malloc.
- >....
- >CreateVector is a simple call to calloc as:
- >
- >typedef float* Vector;
- >
- >Vector CreateVector(int size)
- >{
- > Vector vec;
- >
- > if ((vec = (Vector) calloc(size+1,sizeof(float)))==NULL)
- > WError(10,"Unable to allocate memory for Vector");
- > *vec = (float) size;
- > return(vec);
- >}
- >
- >void FreeVector(Vector vec)
- >{
- > free(vec);
- >}
-
- Clearly there is nothing wrong with the way you allocate and free the
- memory for your vectors, except (nothing to do with your problem):
-
- - malloc rather than calloc would be sufficient and more efficient,
- - depending on the overhead per alloced cell on your system, this might
- make very inefficient use of memory if you have a large number of
- Vectors.
-
- You problem must lie somewhere else in your program. Somewhere you are
- writing beyond the limits of an allocated cell and thus corrupting the
- alloc heap space. These bugs are often difficult to find - try to get
- hold of a memory checking alloc package.
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-